﻿#include "ObjectManager.h"

//IO层
#include "FileReader.h"
#include "FileWriter.h"
#include "SerialPortManager.h"

//UI层
#include "MemsAutoTestSoftware.h"
#include "ParameterConsole.h"
#include "SetMEMSDataDialog.h"
#include "SetPositionModeDialog.h"
#include "SetRateModeDialog.h"
#include "SetScriptDialog.h"
#include "SetScriptLoopDialog.h"
#include "SetScriptWaitDialog.h"
#include "SetThermostatDialog.h"
#include "SetTurnTableAndThermostatDialog.h"

//功能层
#include "MEMSDataCodec.h"
#include "MEMSDataHandler.h"
#include "MEMSDataSaver.h"
#include "ScriptInstruction.h"
#include "ScriptRunner.h"
#include "ScriptSaveLoader.h"
#include "ScriptInstructionFactory.h"
#include "TurnTableCodec.h"
#include "TurnTableConnectionStateManager.h"
#include "TurnTableRunningStateManager.h"
#include "ConfigSaveLoader.h"
#include "LogSaver.h"

ObjectManager::ObjectManager(QObject* parent):QObject(parent)
{
  initModules();
  moveToThread();
  objectInjection();
  configSaveLoader->slot_loadConfig();
  memsAutoTestSoftware->show();
}
ObjectManager::~ObjectManager()
{
  configSaveLoader->slot_saveConfig();
  delete memsAutoTestSoftware;
  for (QThread* pointer : threadPointerList)
    {//析构线程内的对象和线程本身，如果两者存在
      pointer->quit();
    }
  delete qThreadPool;
}
void ObjectManager::initModules()
{
  memsAutoTestSoftware = new MemsAutoTestSoftware();
  //初始化IO
  fileReader = new FileReader(memsAutoTestSoftware);
  fileWriter = new FileWriter(memsAutoTestSoftware);
  //初始化各个串口
  serialPortManager_turnTable = new SerialPortManager();//转台串口管理器
  serialPortManager_turnTable->setObjectName("serialPortManager_turnTable");
  serialPortManager_mEMSData = new SerialPortManager();//数据采集器串口管理器
  serialPortManager_mEMSData->setObjectName("serialPortManager_mEMSData");
  //初始化各UI
  parameterConsole = new ParameterConsole(memsAutoTestSoftware);
  parameterConsole->setObjectName("parameterConsole");
  setMEMSDataDialog = new SetMEMSDataDialog(memsAutoTestSoftware);
  setMEMSDataDialog->setObjectName("setMEMSDataDialog");
  setTurnTableAndThermostatDialog = new SetTurnTableAndThermostatDialog(memsAutoTestSoftware);
  setTurnTableAndThermostatDialog->setObjectName("setTurnTableAndThermostatDialog");
  setScriptDialog = new SetScriptDialog(memsAutoTestSoftware);
  setScriptDialog->setObjectName("setScriptDialog");
  setPositionModeDialog = new SetPositionModeDialog(setScriptDialog);
  setPositionModeDialog->setObjectName("setPositionModeDialog");
  setRateModeDialog = new SetRateModeDialog(setScriptDialog);
  setRateModeDialog->setObjectName("setRateModeDialog");
  setScriptLoopDialog = new SetScriptLoopDialog(setScriptDialog);
  setScriptLoopDialog->setObjectName("setScriptLoopDialog");
  setScriptWaitDialog = new SetScriptWaitDialog(setScriptDialog);
  setScriptWaitDialog->setObjectName("setScriptWaitDialog");
  setThermostatDialog = new SetThermostatDialog(setScriptDialog);
  setThermostatDialog->setObjectName("setThermostatDialog");
  //初始化各核心组件
  mEMSDataCodec = new MEMSDataCodec(memsAutoTestSoftware);//数据采集器编码解码器
  mEMSDataCodec->setObjectName("mEMSDataCodec");
  mEMSDataHandler = new MEMSDataHandler(memsAutoTestSoftware);
  mEMSDataHandler->setObjectName("mEMSDataHandler");
  mEMSDataSaver = new MEMSDataSaver(memsAutoTestSoftware);
  mEMSDataSaver->setObjectName("mEMSDataSaver");
  scriptRunner = new ScriptRunner(memsAutoTestSoftware);
  scriptRunner->setObjectName("scriptRunner");
  scriptSaveLoader = new ScriptSaveLoader(memsAutoTestSoftware);
  scriptSaveLoader->setObjectName("scriptSaveLoader");
  turnTableCodec = new TurnTableCodec(memsAutoTestSoftware);//转台编码解码器
  turnTableCodec->setObjectName("turnTableCodec");
  turnTableConnectionStateManager = new TurnTableConnectionStateManager(memsAutoTestSoftware);
  turnTableConnectionStateManager->setObjectName("turnTableConnectionStateManager");
  turnTableRunningStateManager = new TurnTableRunningStateManager(memsAutoTestSoftware);
  turnTableRunningStateManager->setObjectName("turnTableRunningStateManager");
  configSaveLoader = new ConfigSaveLoader(memsAutoTestSoftware);
  configSaveLoader->setObjectName("configSaveLoader");
  logSaver = new LogSaver(memsAutoTestSoftware);
  logSaver->setObjectName("logSaver");

  //初始化各个线程
  qThread_serialPortManager_turnTable = new QThread();//转台串口管理器线程
  addThreadToList(qThread_serialPortManager_turnTable);
  qThread_serialPortManager_dataReader = new QThread();//数据采集器的串口管理器线程
  addThreadToList(qThread_serialPortManager_dataReader);
  qThreadPool = new QThreadPool();
}
void ObjectManager::objectInjection()
{
  //对象注入
  fileReader->setQThreadPool(qThreadPool);
  fileWriter->setQThreadPool(qThreadPool);
  serialPortManager_turnTable->setLogSaver(logSaver);
  serialPortManager_mEMSData->setLogSaver(logSaver);

  memsAutoTestSoftware->setTurnTableRunningStateManager(turnTableRunningStateManager);
  memsAutoTestSoftware->setTurnTableCodec(turnTableCodec);
  memsAutoTestSoftware->setMEMSDataCodec(mEMSDataCodec);
  memsAutoTestSoftware->setMEMSDataHandler(mEMSDataHandler);
  memsAutoTestSoftware->setMEMSDataSaver(mEMSDataSaver);
  memsAutoTestSoftware->setScriptRunner(scriptRunner);
  memsAutoTestSoftware->setScriptSaveLoader(scriptSaveLoader);
  memsAutoTestSoftware->setLogSaver(logSaver);
  memsAutoTestSoftware->setSerialPortManager_turnTable(serialPortManager_turnTable);
  memsAutoTestSoftware->setSerialPortManager_mEMSData(serialPortManager_mEMSData);
  memsAutoTestSoftware->setSetTurnTableAndThermostatDialog(setTurnTableAndThermostatDialog);
  memsAutoTestSoftware->setSetMEMSDataDialog(setMEMSDataDialog);
  memsAutoTestSoftware->setParameterConsole(parameterConsole);
  memsAutoTestSoftware->setSetScriptDialog(setScriptDialog);

  setMEMSDataDialog->setMEMSDataHandler(mEMSDataHandler);
  setMEMSDataDialog->setMEMSDataCodec(mEMSDataCodec);
  setMEMSDataDialog->setMEMSDataSaver(mEMSDataSaver);
  setMEMSDataDialog->setSerialPortManager(serialPortManager_mEMSData);

  setScriptDialog->setSetScriptWaitDialog(setScriptWaitDialog);
  setScriptDialog->setSetScriptLoopDialog(setScriptLoopDialog);
  setScriptDialog->setSetThermostatDialog(setThermostatDialog);
  setScriptDialog->setSetPositionModeDialog(setPositionModeDialog);
  setScriptDialog->setSetRateModeDialog(setRateModeDialog);
  setScriptDialog->setScriptRunner(scriptRunner);

  setTurnTableAndThermostatDialog->setTurnTableCodec(turnTableCodec);
  setTurnTableAndThermostatDialog->setTurnTableRunningStateManager(turnTableRunningStateManager);
  setTurnTableAndThermostatDialog->setSerialPortManager(serialPortManager_turnTable);

  mEMSDataCodec->setSerialPortManager(serialPortManager_mEMSData);
  mEMSDataCodec->setLogSaver(logSaver);

  mEMSDataHandler->setMEMSDataCodec(mEMSDataCodec);
  mEMSDataHandler->setLogSaver(logSaver);

  mEMSDataSaver->setMEMSDataHandler(mEMSDataHandler);
  mEMSDataSaver->setFileWriter(fileWriter);
  mEMSDataSaver->setLogSaver(logSaver);

  ScriptInstruction::setMEMSDataHandler(mEMSDataHandler);
  ScriptInstruction::setScriptRunner(scriptRunner);
  ScriptInstruction::setTurnTableCodec(turnTableCodec);
  ScriptInstruction::setTurnTableRunningStateManager(turnTableRunningStateManager);
  ScriptInstruction::setTurnTableConnectionStateManager(turnTableConnectionStateManager);

  scriptRunner->setTurnTableCodec(turnTableCodec);
  scriptRunner->setLogSaver(logSaver);

  scriptSaveLoader->setFileWriter(fileWriter);
  scriptSaveLoader->setFileReader(fileReader);
  scriptSaveLoader->setScriptRunner(scriptRunner);
  scriptSaveLoader->setLogSaver(logSaver);

  turnTableCodec->setSerialPortManager(serialPortManager_turnTable);
  turnTableCodec->setLogSaver(logSaver);

  turnTableConnectionStateManager->setTurnTableCodec(turnTableCodec);
  turnTableConnectionStateManager->setLogSaver(logSaver);

  turnTableRunningStateManager->setTurnTableCodec(turnTableCodec);

  configSaveLoader->setMemsAutoTestSoftware(memsAutoTestSoftware);
  configSaveLoader->setParameterConsole(parameterConsole);
  configSaveLoader->setSetPositionModeDialog(setPositionModeDialog);
  configSaveLoader->setSetRateModeDialog(setRateModeDialog);
  configSaveLoader->setSetTurnTableAndThermostatDialog(setTurnTableAndThermostatDialog);
  configSaveLoader->setMEMSDataCodec(mEMSDataCodec);
  configSaveLoader->setMEMSDataHandler(mEMSDataHandler);
  configSaveLoader->setMEMSDataSaver(mEMSDataSaver);
  configSaveLoader->setScriptRunner(scriptRunner);
  configSaveLoader->setTurnTableCodec(turnTableCodec);
  configSaveLoader->setLogSaver(logSaver);
  configSaveLoader->setSerialPortManager_turnTable(serialPortManager_turnTable);
  configSaveLoader->setSerialPortManager_mEMSData(serialPortManager_mEMSData);
  configSaveLoader->setFileReader(fileReader);
  configSaveLoader->setFileWriter(fileWriter);

  //logSaver->setFileWriter(fileWriter);
}
void ObjectManager::moveToThread()
{
  moveObjectToThread(serialPortManager_turnTable, qThread_serialPortManager_turnTable);//将转台串口管理器移动到对应线程
  moveObjectToThread(serialPortManager_mEMSData, qThread_serialPortManager_dataReader);//将数据采集器串口管理器移动到对应线程
}
void ObjectManager::moveObjectToThread(QObject* object, QThread* thread)
{
  object->moveToThread(thread);
  connect(thread, &QThread::finished, thread, &QObject::deleteLater);
  connect(thread, &QThread::finished, object, &QObject::deleteLater);
  thread->start();
}
void ObjectManager::addThreadToList(QThread* thread)
{
  threadPointerList.append(thread);
}
